﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.Admin == "undefined") Axis.Admin = {};

Axis.Admin.Banner = {
    Init: function () {
        var thisObj = this;
        thisObj.Grid.Init();
        thisObj.EventClick();
        $("button").button();
        var cmaskdate = new CMaskDate({
            dateTimePicker: true

        });
        cmaskdate.Init($("#txtEndDate,#txtStartDate"));
    },

    Grid: {
        ID: "#GridContents",
        PagesID: "#GridPages",
        Init: function () {
            var thisObj = this;
            var options = {
                url: 'Service/BannerService.ashx?Action=GET&Context=Backend',
                datatype: "json",
                colNames: ['ID', 'Code', 'Name', 'Category', 'Danh mục', 'Hình ảnh', 'Mô tả', 'Link', 'Ngày bắt đầu', 'Ngày kết thúc', 'Thứ tự', 'Ngày tạo', 'Trạng thái', 'Trạng thái', 'Xử lý'
                ],
                colModel: [
                    { name: 'ID', index: 'ID', width: 50, frozen: true, hidden: true, hidedlg: true },
                    { name: 'Code', index: 'Title', width: 100, frozen: true, hidden: true },
                    { name: 'Name', index: 'Title', width: 150, frozen: true },

                     { name: 'CategoryID', index: 'Title', width: 150, hidden: true, frozen: true },
                   { name: 'Category', index: 'Title', width: 100, frozen: true, hidden: true },

                    { name: 'Image', index: 'Image', width: 70, align: 'center', hidden: true },
                     { name: 'Description', index: 'Description', width: 290, align: 'center' },
                    { name: 'Url', index: 'Url', width: 140, align: 'center', hidden: true },

                     {
                         name: 'StartDate', index: 'EndDate',
                         formatter: 'date', formatoptions: {
                             srcformat: "m/d/Y H:i",
                             newformat: "d/m/Y H:i"
                         },
                         width: 120, align: 'center'
                     },
                    {
                        name: 'EndDate', index: 'EndDate',
                        formatter: 'date', formatoptions: {
                            srcformat: "m/d/Y H:i",
                            newformat: "d/m/Y H:i"
                        },
                        width: 120, align: 'center'
                    },
                    { name: 'ZOrder', index: 'ZOrder', width: 60, align: 'center'
                    },
                     {
                         name: 'CreatedDate', index: 'CreatedDate',
                         formatter: 'date', formatoptions: {
                             srcformat: "m/d/Y H:i",
                             newformat: "d/m/Y H:i"
                         },
                         width: 120, align: 'center'
                     },
                    { name: 'StatusString', index: 'Status', width: 70, align: 'center'
                    },
                    { name: 'Status', index: 'Status', width: 70, align: 'center', hidden: true
                    },
                    { name: 'Action', index: 'Action', width: 55, align: 'center' }
                ],
                afterInsertRow: function (rowid, aData, addData) {
                },
                gridComplete: function () {

                },
                onCellSelect: function (rowid, iCol, cellcontent, e) {

                },
                onSelectRow: function (rowid, status, e) {

                },
                ondblClickRow: function (rowid, iRow, iCol, e) {

                },
                afterInsertRow: function (rowid, aData, addData) {

                    var action = $.string.Format("<img class='imgAction' src='../../Images/16x16/edit.png' onclick=\"ShowEdit('{0}')\"/><img class='imgAction' src='../../Images/16x16/cancel.png' onclick=\"DeleteNews('{0}')\"/>", rowid);
                    $(thisObj.ID).jqGrid('setCell', rowid, 'Action', action);
                    var iconStatus = 's_enable.png';
                    if (aData.Status == '-1')
                        iconStatus = 's_delete.png';
                    if (aData.Status == '1')
                        iconStatus = 's_visible.png';
                    var StatusString = $.string.Format("<img class='imgAction' src='../../Images/16x16/{0}' onclick=\"ActionStatus('{1}',{2})\"/>", iconStatus, rowid, aData.Status);
                    $(thisObj.ID).jqGrid('setCell', rowid, 'StatusString', StatusString);
                    $(thisObj.ID).jqGrid('setCell', rowid, 'Image2', $.string.Format("<img class='imgAction' src='../../../{0}'/>", aData.Image));

                },
                multiselect: true,
                height: 'auto',
                rownumbers: true,
                autowidth: true,
                //forceFit: true,
                shrinkToFit: false,
                rowNum: 20,
                pager: thisObj.PagesID
            };
            thisObj.GridObj = new CGrid(options);
            thisObj.GridObj.Init(thisObj.ID);
            thisObj.GridObj.NavGrid();
            thisObj.GridObj.FrozenColumns();
        },
        Reload: function () {
            var thisObj = this;
            if (thisObj.GridObj)
                thisObj.GridObj.ReloadGrid();
        },
        SelectRow: function () {
            var thisObj = this;
            if (thisObj.GridObj) {
                return thisObj.GridObj.SelectRow();
            }
            return null;
        },
        GetRowData: function (id) {
            var thisObj = this;
            return $(thisObj.ID).jqGrid('getRowData', id);
        }
    },
    DialogEdit: {
        ID: "#FormEdit",
        Dialog: null,
        Init: function (action, opts) {
            var thisObj = this;
            thisObj.ClearForm();
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Sửa bài viết",
                    width: 400,
                    position: {
                        my: 'top',
                        at: 'top'
                    },
                    open: function () {

                    }
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
            var buttonText = "";
            switch (action) {
                case "INSERT":
                    thisObj.Dialog.SetTitle("Thêm Banner");
                    $(thisObj.ID).find("[data-role='INSERT']").show();
                    $(thisObj.ID).find("#txtName").focus();
                    buttonText = "Thêm";
                    $("#txtStatus").attr('checked', 'checked');
                    break;
                case "UPDATE":
                    thisObj.Dialog.SetTitle("Sửa Banner: " + opts.Text);
                    $.each(opts, function (key, value) {
                        switch (key) {
                            case 'Status':
                                $("#txtStatus").attr('checked', 'checked');
                                if (value != "0")
                                    $("#txtStatus").removeAttr('checked');
                                break;
                            default:
                                $(thisObj.ID).find("[data-field=" + key + "]").val(value);
                        }
                    });

                    buttonText = "Hiệu chỉnh";
                    break;
            }
            var buttons = [
                        {
                            text: buttonText,
                            click: function (e) {
                                Axis.Admin.Banner.Update(action, opts);
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Đóng",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

            ];
            $(thisObj.ID).dialog('option', 'buttons', buttons);
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        },
        LoadNews: function (opt) {

        },
        ClearForm: function () {
            $("#FormEdit").find("[data-field]").each(function () {
                var $this = $(this);
                $this.val("");
                if ($this.is("select")) {
                    var value = $this.find("option[Default-Value='1']").attr("value");
                    $this.val(value);
                }

            });
            $("#FormEdit").find("[data-role='INSERT']").hide();
        }
    },
    Update: function (type, opts) {
        var thisObj = this;
        var data = {};
        switch (type) {
            case "INSERT":
            case "UPDATE":
                $("#FormEdit").find("[data-field]").each(function () {
                    var $this = $(this);
                    if ($this.attr("data-field")) {
                        var key = $this.attr("data-field");
                        var value = $this.val();
                        if ($this.attr('type') == 'checkbox') {
                            if ($this.is(':checked'))
                                value = 0;
                            else
                                value = 1;
                        }

                        if ($this.attr("role") == "datepicker") {
                            value = $.CFormat.FormatDate(value, "yyyy/MM/dd HH:mm", "vi-VN", "en-US");
                        }

                        data[key] = value;
                    }
                });
                break;
            case "DELETE":
                data.ID = opts.ID;
                break;
        }

        data.Action = type;
        $.Ajax({
            url: 'Service/BannerService.ashx',
            type: 'POST',
            data: data,
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    data = eval("(" + data + ")");
                    if (data.IsSuccessfull) {
                        thisObj.Grid.Reload();
                    }
                }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    },
    DialogFilter: {
        Init: function () { }
    },
    EventClick: function () {
        var thisObj = this;
        $("#btnSelectImg").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.startupPath = "Images:/Banner/";
            finder.selectActionFunction = function (fileUrl) {
                $('#txtImage').val(fileUrl);
            };
            finder.popup();
        });

        $("#imgUserAdd").click(function () {
            thisObj.DialogEdit.Init("INSERT");
        });
        $("#imgUserEdit").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID)
                thisObj.DialogEdit.Init("UPDATE", rowData);
            else alert("Please Select Row");
        });
        $("#imgUserDel").click(function () {
            thisObj.DeleteNews();

        });
        $("#imgUserFilter").click(function () {
            thisObj.DialogFilter.Init();
        });

    },
    DeleteNews: function (id) {
        var thisObj = this;
        var rowData = thisObj.Grid.SelectRow();
        if (typeof id != 'undefined')
            rowData = thisObj.Grid.GetRowData(id);
        if (rowData && rowData.ID) {
            $.CAlert.Confirm({
                Title: "Xóa Tabs",
                Content: $.string.Format("Bạn có chắc muốn xóa Tabs: [{0}].", rowData.Title)
            }, function (isOK) {
                if (isOK)
                    thisObj.Update("DELETE", rowData);
            });
        }
        else {
            $.CAlert.Alert({
                Title: "Xóa Tabs",
                Content: "Vui lòng chọn Tabs cần xóa."
            });
        }
    }

};
$(function () {
    Axis.Admin.Banner.Init();
});
function ShowEdit(rowID) {
    var thisObj = Axis.Admin.Banner;
    var rowData = $(thisObj.Grid.ID).jqGrid('getRowData', rowID);
    if (rowData && rowData.ID)
        thisObj.DialogEdit.Init("UPDATE", rowData);
}
function DeleteNews(rowID) {
    Axis.Admin.Banner.DeleteNews(rowID);
}
function FormatGridDescription(cellvalue, options, rowObject) {
    if (cellvalue.indexOf("project_desc_panel") == -1) {
        return '<div class="project_desc_panel">' + cellvalue + '</div>';
    }
    else
        return cellvalue;

}